package com.skstudio.reportview;

import com.skstudio.report.ReportProcess;
import com.skstudio.report.SpyData;

public class ReporterProcessMemory extends ReportViewController {

    ReportProcess mProc = null;

    @Override
    public void setSpyData(SpyData ref) {
        super.setSpyData(ref);
        mProc = ref.getReportProcess();
    }

    @Override
    public int getReportRows() {
        return 1;
    }

    @Override
    public int getReportColumns() {
        return mProc.getDataSize();
    }

    @Override
    public int getEntry(int row, int col) {
        if (row == 0 && mProc.mDataList.size() > col) {
            float delta = (float) ((mProc.mDataList.get(col).mRSS - mProc.mInitRSS) * 4096 / 1024);

            float value = 0.f;
            if (delta < -100 * 1024) {
                value = 0.f;
            } else if (delta < -10 * 1024) {
                value = 10 * ((delta * -1 - 10 * 1024) / ((100 - 10) * 1024));
            } else if (delta < -5 * 1024) {
                value = 10 + 10 * ((delta * -1 - 5 * 1024) / ((10 - 5) * 1024));
            } else if (delta < -1 * 1024) {
                value = 20 + 10 * ((delta * -1 - 1 * 1024) / ((5 - 1) * 1024));
            } else if (delta < -500) {
                value = 30 + 10 * ((delta * -1 - 500) / (1024 - 500));
            } else if (delta < 0) {
                value = 40 + 10 * (delta / 500);
            } else if (delta < 500) {
                value = 50 + 10 * (delta / 500);
            } else if (delta < 1 * 1024) {
                value = 60 + 10 * ((delta - 500) / (1024 - 500));
            } else if (delta < 5 * 1024) {
                value = 70 + 10 * ((delta - 1 * 1024) / ((5 - 1) * 1024));
            } else if (delta < 10 * 1024) {
                value = 80 + 10 * ((delta - 5 * 1024) / ((10 - 5) * 1024));
            } else if (delta < 100 * 1024) {
                value = 90 + 10 * ((delta - 10 * 1024) / ((100 - 10) * 1024));
            } else {
                value = 100.0f;
            }
            return (int) value;
        }
        return 0;
    }

    /*@Override
    public String getEntryName(int row) {
    switch (row) {
    case 0:
    return "The amount of virtual memory used";
    }
    return null;
    }*/
    @Override
    public String stringYLabels(int id) {
        switch (id) {
            case 0:
                return "-100MB+";
            case 1:
                return "-10MB";
            case 2:
                return "-5MB";
            case 3:
                return "-1MB";
            case 4:
                return "-500K";
            case 5:
                return "0";
            case 6:
                return "500K";
            case 7:
                return "1MB";
            case 8:
                return "5MB";
            case 9:
                return "10MB";
            case 10:
                return "100MB+";
        }
        return null;
    }

    @Override
    public String getEntryHint(int col) {
        if (mProc.mDataList.size() > col) {
            long now = mProc.mDataList.get(col).mRSS * 4096 / 1024;
            long init = mProc.mInitRSS * 4096 / 1024;
            String hint = String.format("Memory delta %dKB from %dKB (Max %dKB, Min %dKB)", now - init, init,
                    mProc.mMaxRSS, mProc.mMinRSS);
            return hint;
        }
        return null;
    }

    public long getEntryMemUsage(int col) {
        return mProc.mDataList.get(col).mRSS * 4096 / 1024;
    }
}
